<?php

namespace app\api\model;

use think\Model;

class ExaminationQuestion extends Model
{
    public static function getAvgDifficultyByPid($pid)
    {
        return self::alias('a')
            ->join('questions_list b', 'a.qid = b.id')
            ->where(['a.pid' => $pid])
            ->avg('difficulty');
    }

    public static function getCountAndTotalByPid($pid)
    {
        return self::where(['pid' => $pid])->field('count(*) as numbers,sum(score) as scores')->find();
    }

    public static function getQuestionListByPid($pid)
    {
        $list = self::alias('a')
            ->join('questions_list b', 'a.qid = b.id')
            ->where(['a.pid' => $pid])
            ->field('b.*,a.score')
            ->select()
            ->toArray();
        $data = [];
        foreach ($list as $key => $item) {
            list($answers, $correct) = QuestionsAnswers::getQuestionAnswerByQid($item['id']);
            $data[] = [
                'type' => QuestionsList::$typeList[$item['type']]['type'] . '-group',
                'category' => 'container',
                'icon' => 'group',
                'widgetList' => [
                    [
                        'id' => $item['id'],
                        'type' => QuestionsList::$typeList[$item['type']]['type'],
                        'icon' => QuestionsList::$typeList[$item['type']]['type'] . '-field',
                        'formItemFlag' => true,
                        'options' => [
                            'topic' => $item['title'],
                            'score' => $item['score'],
                            'index' => $key + 1,
                            'correct' => $item['type'] == 2 ? $correct : implode($correct),
                            'errorRes' => '无',
                            'name' => (string)$item['id'],
                            'label' => QuestionsList::$typeList[$item['type']]['type'],
                            'optionItems' => $answers
                        ],
                    ],
                ],
            ];
        }
        return $data;
    }
}